//
//  ContentScrollView.m
//  BeautifulLockerRoom
//
//  Created by mac on 16/7/28.
//  Copyright © 2016年 Winny. All rights reserved.
//

#import "ContentScrollView.h"
#import "MidView.h"
#import "TopView.h"
#import "menuScrollView.h"



@interface ContentScrollView ()<UIScrollViewDelegate>
{

    NSMutableArray * valueArray;
    NSArray * _dataArray;
    NSInteger _tagInfo;
    NSInteger offest;
}

@end

@implementation ContentScrollView

-(ContentCollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.itemSize = CGSizeMake(WINDOWWIDTH/2-4, WINDOWWIDTH/2+60+30);
        layout.headerReferenceSize = CGSizeMake(WINDOWWIDTH, 720);
        layout.minimumLineSpacing = 2;
        layout.minimumInteritemSpacing = 2;
        layout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
        
        _collectionView = [[ContentCollectionView alloc]initWithFrame:CGRectMake(0, 0, WINDOWWIDTH, WINDOWHEIGHT - 108) collectionViewLayout:layout];
        
    }
    return _collectionView;
}
-(OtherCollectionView *)otherCollectionView
{
    if (!_otherCollectionView)
    {
        
        _otherCollectionView = [[OtherCollectionView alloc]initWithFrame:CGRectMake(0, 0, WINDOWWIDTH, WINDOWHEIGHT - 108)];
        NSLog(@"!!!!%f!!!!",_otherCollectionView.viewHeight);
        
    }
    
    return _otherCollectionView;
}
-(instancetype)init
{
    self = [super init];
    if (self) {
        self = [[ContentScrollView alloc]initWithFrame:CGRectMake(0, 104, WINDOWWIDTH, WINDOWHEIGHT - 148)];
        [self contentSVChildView];
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.pagingEnabled = YES;
        self.directionalLockEnabled = YES;
        [self addSubview:self.collectionView];
        self.scrollsToTop = YES;
        self.delegate = self;
        
      
    }
    return self;
}
-(void)getContentScrollViewContentSize
{
    self.contentSize = CGSizeMake(valueArray.count*WINDOWWIDTH,0);
}
-(void)contentSVChildView
{
    [self getModelArray];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    [menuScrollView contentVCMoveWithOffset:scrollView.contentOffset.x];
    if (self.contentOffset.x > 10)
    {
        offest = self.contentOffset.x/WINDOWWIDTH;
        int a = self.contentOffset.x;
        int b = WINDOWWIDTH;
        if (a % b == 0)
        {
            [self addSubview:self.otherCollectionView];
            [RequestTool getAllModelWithValue:valueArray[offest] data:^(NSMutableArray *array) {
                self.otherCollectionView.dataArray = array;               
                _otherCollectionView.frame = CGRectMake(offest * WINDOWWIDTH, 0, WINDOWWIDTH, WINDOWHEIGHT-113);
                [RequestTool getOtherAllModelWithValue:valueArray[offest] data:^(NSMutableArray *array1) {
                    _otherCollectionView.headArray = array1;
                    for (DataModel * model in array1) {
                    }
                    [self.otherCollectionView reloadData];
                }];
            }];
            
            
            
        }
       // NSNotification * notification = [NSNotification notificationWithName:@"notice1" object:nil userInfo:@{@"tag":@(offest)}];
        //[[NSNotificationCenter defaultCenter] postNotification:notification];
    }
}
-(void)getModelArray
{
    valueArray = [[NSMutableArray alloc]init];
    [RequestTool getNetworkRequestWithUrl:MAINMENUURL date:^(NSDictionary *dic) {
        NSDictionary * data = [dic objectForKey:@"data"];
        NSArray * floor_items = [data objectForKey:@"floor_items"];
        
        for (int i = 0; i < floor_items.count; i++)
        {
            NSDictionary * items = floor_items[i];
            NSArray * content_items = [items objectForKey:@"content_items"];
            NSDictionary * item = content_items.lastObject;
            NSArray * request_parameters = [item objectForKey:@"request_parameters"];
            NSDictionary * itemValue = request_parameters[1];
            NSString * value = [itemValue objectForKey:@"value"];            
            [valueArray addObject:value];
        }
        [RequestTool getAllModelWithValue:valueArray[0] data:^(NSMutableArray *array) {
            _collectionView.dataArray = array;
            [_collectionView reloadData];
        }];
        
        [self getContentScrollViewContentSize];
    }];
}

@end
